package demo1;

import java.util.*;

public class Main {

    public static int N, n, m;
    public static int[] dx = {0,0,1,-1};
    public static int[] dy = {1,-1,0,0};
    public static int[][] arr = new int[N][N];
    public static int[][] dp = new int[N][N];

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        n = in.nextInt(); m = in.nextInt();
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                arr[i][j] = in.nextInt();
            }
        }

        int ret = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                ret = Math.max(ret, dfs(i, j));
            }
        }
        System.out.println(ret);
    }

    public static int dfs(int i, int j){
        if(dp[i][j] != 0) { return dp[i][j];}

        int len = 1;
        for(int k = 0; k < 4; k++){
            int x = i + dx[k], y = j + dy[k];
            if(x >= 0 && x < n && y >= 0 && y < m && arr[x][y] < arr[i][j]){
                len = Math.max(len, dfs(x, y) + 1);
            }
        }
        dp[i][j] = len;
        return len;
    }
}
